﻿using System;
using System.Data;

namespace SimpleService.ORM
{
    public class Insert
    {
        public PersistenceObject _table;

        public Insert(PersistenceObject _table) { this._table = _table; }	

        private string getColumnsDefaultValueString(string key)
        {
            string ans = "";
            string _key = "";

            //先判斷「主鍵」為int或String，判斷是否加上''
            if (DefaultValue(0) == "0")
            { _key = key; }
            else
            { _key = PersistenceUtility.isValue(key); }

            //如果主鍵有東西，就加上「,」
            if (!PersistenceUtility.isNULL(_key))
            { ans = _key + ","; }

            //組出後面全部欄位預設值
            for (int i = 1; i < _table.ColsCount; i++)
            {
                //欄位是最後一個，就不加「,」
                if (i == _table.ColsCount - 1)
                { ans += DefaultValue(i); }

                //欄位不是最後一個，就加上「,」
                else
                { ans += DefaultValue(i) + ","; }
            }
            return ans;
        }


        private string DefaultValue(int i)
        {
            return getDefaultValue(_table.ColsType[i]);
        }

  
        private string getDefaultValue(string s)
        {
            string ans = "";

            if (s == "System.Int32")
            { ans = "0"; }

            else
            { ans = "''"; }

            return ans;
        }

  
        private string getColumnsNameString(int beginValue)
        {
            string ans = "";
            //組出全部欄位字串
            for (int i = beginValue; i < _table.ColsCount; i++)
            {
                //欄位是最後一個，就不加		
                if (i == _table.ColsCount - 1)
                { ans += PersistenceUtility.isField(_table.ColsName[i]); }
                //欄位不是最後一個，就加上「,」
                else
                { ans += PersistenceUtility.isField(_table.ColsName[i]) + ","; }
            }
            return ans;
        }



        public void Execution()
        {
            try
            {
                _table.RowsCount = PersistenceUtility.RowsCount(_table.datatable); //取得總筆數
                _table.ColsCount = PersistenceUtility.ColumnsCount(_table.datatable); //取得欄位總數
                _table.ColsName = PersistenceUtility.ColumnsName(_table.datatable); //取得所有欄位名稱
                _table.ColsType = PersistenceUtility.ColumnsType(_table.datatable); //取得所有欄位型態		

            
                string SQLstr = ORMConfig.insert; 
                SQLstr = SQLstr.Replace("__table__", _table._TableName);
                SQLstr = SQLstr.Replace("__field__", getColumnsNameString(1)); //主鍵欄位不抓
                SQLstr = SQLstr.Replace("__fval__", getColumnsDefaultValueString("")); //主鍵值不抓
                
                SqlOperator.ExecuteNonQuery(SQLstr, _table._connection);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <param name="主鍵值">使用者輸入的字串，輸入「主鍵值」。</param>
        public void Execution(string pkey)
        {
            try
            {
                _table.RowsCount = PersistenceUtility.RowsCount(_table.datatable); //取得總筆數
                _table.ColsCount = PersistenceUtility.ColumnsCount(_table.datatable); //取得欄位總數
                _table.ColsName = PersistenceUtility.ColumnsName(_table.datatable); //取得所有欄位名稱
                _table.ColsType = PersistenceUtility.ColumnsType(_table.datatable); //取得所有欄位型態			

                //SQL字串
                string SQLstr = ORMConfig.insert;
                SQLstr = SQLstr.Replace("__table__", _table._TableName);
                SQLstr = SQLstr.Replace("__field__", getColumnsNameString(0));
                SQLstr = SQLstr.Replace("__fval__", getColumnsDefaultValueString(pkey));
                
                SqlOperator.ExecuteNonQuery(SQLstr, _table._connection);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}
